class SoundManager {
	private static _manager: SoundManager;
	public static get Instance() {
		if (!SoundManager._manager) {
			SoundManager._manager = new SoundManager();
		}
		return SoundManager._manager;
	}
	private bgMusic: egret.Sound;
	private bgMusic_channel: egret.SoundChannel;
	private isbgComplete: boolean = false;	//背景音乐是否加载完成


	private YX: egret.Sound;
	private YX_channel: egret.SoundChannel;

	private bomM: egret.Sound;
	private bomM_cl: egret.SoundChannel;
	private isbomComplete: boolean = false;

	private _isMusic: boolean = true;	//背景音乐是否播放
	private _isEffect: boolean = true;	//音效是否播放

	private _isvolume: number = 1;

	public constructor() {
		this.bgMusic = new egret.Sound;
		this.bgMusic.load("resource/music1.mp3");
		this.bgMusic.addEventListener(egret.Event.COMPLETE, () => {
			this.isbgComplete = true;
		}, this);

		this.YX = new egret.Sound;
		this.YX.load("resource/assets/wrong.mp3");
		// this.YX.addEventListener(egret.Event.COMPLETE,()=>{
		// 	this.isbgComplete = true;
		// },this);

		this.bomM = new egret.Sound;
		this.bomM.load('resource/assets/right.mp3');
		this.bomM.addEventListener(egret.Event.COMPLETE, () => {
			this.isbomComplete = true;
		}, this);
	}
	//播放背景音乐
	private cs: number = 1;	//控制背景音乐不会重复添加
	public playbg() {
		if (this.isbgComplete && this.isMusic && this.cs) {
			this.bgMusic_channel = this.bgMusic.play();
			this.cs = 0;
			this.bgMusic_channel.volume = this.isvolume;
		}
	}
	//停止背景音乐
	public stopbg() {
		if (this.bgMusic_channel) {
			this.bgMusic_channel.stop();
			this.cs = 1;
			this.bgMusic_channel = null;
		}
	}
	//播放音效
	public bom() {
		if (this.isbomComplete && this.isEffect) {
			this.bomM_cl = this.bomM.play(0, 1);

		}
	}
	public bom1() {
		if (this.isbomComplete && this.isEffect) {
			this.YX_channel = this.YX.play(0, 1);
		}
	}

	//设置音量
	public setVolume(s: number) {
		if (this.bgMusic_channel) {
			this.bgMusic_channel.volume = s;
		}

	}

	public get isMusic() {
		let ret = egret.localStorage.getItem("isMusic");
		if (ret == "true" || !ret) {
			this._isMusic = true;
		} else {
			this._isMusic = false;
		}
		return this._isMusic;
	}
	public set isMusic(b: boolean) {
		this._isMusic = b;
		egret.localStorage.setItem("isMusic", b.toString());
		if (b) {
			//播放
			this.playbg();
		} else {
			//关闭
			this.stopbg();
		}
	}
	public get isEffect() {
		let ret = egret.localStorage.getItem("isEffect");
		if (ret == "true" || !ret) {
			this._isEffect = true;
		} else {
			this._isEffect = false;
		}
		return this._isEffect;
	}
	public set isEffect(b: boolean) {
		this._isEffect = b;
		egret.localStorage.setItem("isEffect", b.toString());
		if (b == false) {
			//停止播放音效
			this.bomM_cl.stop();
			this.YX_channel.stop();
		} else {
			this.bom();
			this.bom1();
			//this.YX()
		}
	}
	public set isvolume(v: number) {
		this._isvolume = v;
		egret.localStorage.setItem('isvolume', v.toString());
		this.setVolume(v);
	}
	public get isvolume() {
		this._isvolume = Number(egret.localStorage.getItem("isvolume"));
		if (!this._isvolume) {
			this._isvolume = 1;
		}
		return this._isvolume;
	}
}